﻿using System;
using Krilib.Bridges;

namespace Krile.Subsystem
{
    public static class CommonOperation
    {
        public static void CallBrowser(Uri uri)
        {
            try
            {
                if (String.IsNullOrEmpty(Core.Config.Network.BrowserPath))
                    System.Diagnostics.Process.Start(uri.OriginalString);
                else
                    System.Diagnostics.Process.Start(Core.Config.Network.BrowserPath, uri.OriginalString);
            }
            catch (System.ComponentModel.Win32Exception) { }
            catch (Exception e)
            {
                Subsystem.Debugger.AddReport(e);
            }
        }

        internal static void CheckUpdate()
        {
        }
    }

    public class __CommmonOperationBridge : ICommonOperation
    {
        public void CallBrowser(Uri uri)
        {
            try
            {
                if (String.IsNullOrEmpty(Core.Config.Network.BrowserPath))
                    System.Diagnostics.Process.Start(uri.OriginalString);
                else
                    System.Diagnostics.Process.Start(Core.Config.Network.BrowserPath, uri.OriginalString);
            }
            catch (System.ComponentModel.Win32Exception) { }
            catch (Exception e)
            {
                Subsystem.Debugger.AddReport(e);
            }
        }

        public string AESDecrypt(string cryptograph)
        {
            return Subsystem.Security.DecryptString(cryptograph);
        }

        public string AESEncrypt(string source)
        {
            return Subsystem.Security.EncryptString(source);
        }

        public Type XMLLoad<Type>(string path)
        {
            return Krile.Imported.Snippets.Files.LoadXML<Type>(path, true);
        }

        public void XMLSave<Type>(Type obj, string path)
        {
            Krile.Imported.Snippets.Files.SaveXML<Type>(path, obj);
        }

        public void SetCursor(System.Windows.Forms.Cursor cursor)
        {
            Core.FormManager.MainForm.SetCursor(cursor);
        }

        public Krilib.Data.SearchDataCluster ConfigSearchData(Krilib.Data.SearchDataCluster cluster)
        {
            using (var c = new Forms.Dialogs.Editor.SearchData.SearchDataClusterEditor(cluster))
            {
                if (c.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    return c.SearchDataCluster;
                else
                    return null;
            }
        }
    }
}
